﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenExpressApp.MetaModel;
using System.Windows.Controls;


namespace OpenExpressApp.Module.WPF.Editors
{
    /// <summary>
    /// Column Factory
    /// </summary>
    public class GridColumnsFactory
    {
        /// <summary>
        /// 默认的列类型和column编辑器的映射关系
        /// </summary>
        private static Dictionary<string, Type> gridColumnMap = new Dictionary<string, Type>() { 
            { EditorNames.String, typeof(GDataGridStringColumn) },
            { EditorNames.Enum, typeof(GDataGridEnumColumn) },
            { EditorNames.FileData, typeof(GDataGridFileDataColumn) },
            { EditorNames.LookupDropDown, typeof(GDataGridLookupListColumn) },
            { EditorNames.Memo, typeof(GDataGridMemoColumn)},
            { EditorNames.Boolean,typeof(GDataGridBooleanColumn)},
        };

        /// <summary>
        /// 生成一个可用于编辑info的列编辑器
        /// </summary>
        /// <param name="editorName"></param>
        /// <param name="info"></param>
        /// <param name="view"></param>
        /// <returns></returns>
        public static DataGridColumn GetDefaultColumn(
            string editorName,
            BusinessObjectPropertyInfo info,
            IListObjectView view
            )
        {
            var columnEditorType = GetColumnType(editorName);

            //这些类型都需要info和view作为构造器的参数
            return (DataGridColumn)Activator.CreateInstance(columnEditorType, new object[] { info, view });
        }
        /// <summary>
        /// 添加editorName到gridColumnType的映射
        /// </summary>
        /// <param name="name"></param>
        /// <param name="type"></param>
        public static void AddEditor(string name, Type type)
        {
            gridColumnMap.Add(name, type);
        }

        /// <summary>
        /// 默认使用string的编辑器
        /// </summary>
        /// <returns></returns>
        private static Type GetDefaultPropertyEditorType()
        {
            return gridColumnMap[EditorNames.String];
        }
        /// <summary>
        /// 根据名字获取列编辑器
        /// </summary>
        /// <param name="editorName"></param>
        /// <returns></returns>
        private static Type GetColumnType(string editorName)
        {
            Type editor = null;
            gridColumnMap.TryGetValue(editorName, out editor);
            return editor ?? GetDefaultPropertyEditorType();
        }
    }
}
